package com.ypc.webdemo.run;

import com.ypc.webdemo.common.PluginStatusEnum;
import com.ypc.webdemo.dao.entity.Plugin;
import com.ypc.webdemo.dao.mapper.PluginMapper;
import com.ypc.webdemo.service.PluginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author yinpengcheng
 * @Date 2023/7/25
 */
@Slf4j
@Component
public class InitPlugins implements CommandLineRunner {
    @Resource
    private PluginService pluginService;
    @Resource
    private PluginMapper pluginMapper;

    @Override
    public void run(String... args) throws Exception {
        List<Plugin> plugins = pluginMapper.findByStatusIn(Arrays.asList(PluginStatusEnum.installed.name(), PluginStatusEnum.uninstall_failed.name()));
        plugins.forEach(plugin -> pluginService.install(plugin));
    }
}
